<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- three.js library -->
<script src='vendor/three.js/build/three.min.js'></script>
<body style='margin : 0px; overflow: hidden; font-family: Monospace;'><div style='position: absolute; top: 10px; width:100%; text-align: center; z-index: 1;'>
	<a href="https://github.com/jeromeetienne/AR.js/" target="_blank">AR.js</a> - three.js camera transform
	<br/>
	Contact me any time at <a href='https://twitter.com/jerome_etienne' target='_blank'>@jerome_etienne</a>
</div><script>
	//////////////////////////////////////////////////////////////////////////////////
	//		Init
	//////////////////////////////////////////////////////////////////////////////////

	// init renderer
	var renderer	= new THREE.WebGLRenderer({
		antialias: true,
		alpha: true
	});
	renderer.setSize( window.innerWidth, window.innerHeight );
	renderer.domElement.style.position = 'absolute'
	renderer.domElement.style.top = '0px'
	renderer.domElement.style.left = '0px'
	document.body.appendChild( renderer.domElement );

	// array of functions for the rendering loop
	var onRenderFcts= [];

	// init scene and camera
	var scene	= new THREE.Scene();
		
	//////////////////////////////////////////////////////////////////////////////////
	//		Initialize a basic camera
	//////////////////////////////////////////////////////////////////////////////////


	// Create a camera
	var camera = new THREE.PerspectiveCamera( 23*2, window.innerWidth / window.innerHeight, 0.01, 100 );
	camera.position.z = 4;
	window.addEventListener( 'resize', function() {
		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();
		
		renderer.setSize( window.innerWidth, window.innerHeight );
	}, false );
			
	//////////////////////////////////////////////////////////////////////////////////
	//		add an object in the scene
	//////////////////////////////////////////////////////////////////////////////////

	// add a torus knot	
	var geometry	= new THREE.CubeGeometry(1,1,1);
	var material	= new THREE.MeshNormalMaterial({
		transparent : true,
		opacity: 0.5,
		side: THREE.DoubleSide
	}); 
	var meshCube	= new THREE.Mesh( geometry, material );
	scene.add( meshCube );
	
	meshCube.add( new THREE.AxisHelper() )
	
	var geometry	= new THREE.TorusKnotGeometry(0.3,0.1,64,16);
	var material	= new THREE.MeshNormalMaterial(); 
	var mesh	= new THREE.Mesh( geometry, material );
	meshCube.add( mesh );
	
	onRenderFcts.push(function(delta){
		mesh.rotation.x += Math.PI*delta
	})

	window.addEventListener("deviceorientation", function onDeviceOrientation(event) {
		var deviceEuler = new THREE.Euler()
		deviceEuler.x =  event.beta  / 180 * Math.PI
		deviceEuler.y =  event.gamma / 180 * Math.PI
		deviceEuler.z =  event.alpha / 180 * Math.PI
		deviceEuler.order = "XZY"
	
		var controllerQuaternion = new THREE.Quaternion().setFromEuler(deviceEuler).inverse()			
		meshCube.quaternion.copy(controllerQuaternion)			
	});


	// window.addEventListener("deviceorientation", function onDeviceOrientation(event) {
	// 	var deviceEuler = new THREE.Euler()
	// 	deviceEuler.x =  event.beta  / 180 * Math.PI
	// 	deviceEuler.y =  event.gamma / 180 * Math.PI
	// 	deviceEuler.z =  event.alpha / 180 * Math.PI
	// 	deviceEuler.order = "XZY"
	// 
	// 	var controllerQuaternion = new THREE.Quaternion().setFromEuler(deviceEuler).inverse()			
	// 	meshCube.quaternion.copy(controllerQuaternion)			
	// });
	// 


	// window.addEventListener("devicemotion", function onDeviceMotion(event) {
	// 	// document.querySelector('#accelerationIncludingGravityX').innerHTML = event.accelerationIncludingGravity.x.toFixed(2)
	// 	// document.querySelector('#accelerationIncludingGravityY').innerHTML = event.accelerationIncludingGravity.y.toFixed(2)
	// 	// document.querySelector('#accelerationIncludingGravityZ').innerHTML = event.accelerationIncludingGravity.z.toFixed(2)
	// 	// 
	// 	// document.querySelector('#accelerationX').innerHTML = event.acceleration.x.toFixed(2)
	// 	// document.querySelector('#accelerationY').innerHTML = event.acceleration.y.toFixed(2)
	// 	// document.querySelector('#accelerationZ').innerHTML = event.acceleration.z.toFixed(2)
	// 	// 
	// 	// document.querySelector('#gravityX').innerHTML = (event.accelerationIncludingGravity.x - event.acceleration.x).toFixed(2)
	// 	// document.querySelector('#gravityY').innerHTML = (event.accelerationIncludingGravity.y - event.acceleration.y).toFixed(2)
	// 	// document.querySelector('#gravityZ').innerHTML = (event.accelerationIncludingGravity.z - event.acceleration.z).toFixed(2)
	// 	// 
	// 	var gravity = new THREE.Vector3()
	// 	gravity.x = event.accelerationIncludingGravity.x - event.acceleration.x
	// 	gravity.y = event.accelerationIncludingGravity.y - event.acceleration.y
	// 	gravity.z = event.accelerationIncludingGravity.z - event.acceleration.z
	// 
	// 	meshCube.lookAt(gravity)
	// });


	//////////////////////////////////////////////////////////////////////////////////
	//		render the whole thing on the page
	//////////////////////////////////////////////////////////////////////////////////

	// render the scene
	onRenderFcts.push(function(){
		renderer.render( scene, camera );
	})

	// run the rendering loop
	var lastTimeMsec= null
	requestAnimationFrame(function animate(nowMsec){
		// keep looping
		requestAnimationFrame( animate );
		// measure time
		lastTimeMsec	= lastTimeMsec || nowMsec-1000/60
		var deltaMsec	= Math.min(200, nowMsec - lastTimeMsec)
		lastTimeMsec	= nowMsec
		// call each update function
		onRenderFcts.forEach(function(onRenderFct){
			onRenderFct(deltaMsec/1000, nowMsec/1000)
		})
	})
</script></body>
